<?php

require_once 'spElement.php';
require_once "spRequestManager.php";

class spInput extends spElement
{
	public function __construct( $param=null )
	{
		parent::__construct( "input", $param );
		parent::setMustEndTag( true );
		
		if ( isset($param["type"]) )
			$this->setType( $param["type"] );
			
		if ( isset($param["maxlength"]) && $param["maxlength"]!="" )
		{
			parent::addAttr( "maxlength", $param["maxlength"] );
			parent::addAttr( "size", $param["maxlength"] );
		}		
	}
	
	public function setType( $type )
	{
		parent::addAttr( "type", $type );
	}
	
	public function addData( $key, $value )
	{
		$data = parent::addData( $key, $value );

		if ( isset($data["name"]) )
			parent::addAttr( "name", $data["name"] );

		if ( isset($data["value"]) )
			parent::addAttr( "value", $data["value"] );
	}
	
	public function setName( $name )
	{
		parent::addAttr( "name", $name );
	}
	
	public function getName()
	{
		return parent::getAttr( "name" );
	}
	
	public function setValue( $v )
	{
		parent::addData( "value", $v );
		parent::addAttr( "value", $v );
	}
	
	public function getValue()
	{
		return parent::getAttr( "value" );
	}
	
	public function render( $indent=0 )
	{
		if ( parent::readOnly() )
		{
			parent::addAttr( "readonly", "readonly" );
			parent::addAttr( "disabled", "disabled" );
		}
		
		parent::render( $indent );
	}
	
	public function setElementDataString( $s )
	{
		parent::setElementDataString( $s );	
		$this->setValue( $s );
	}
	
	public function updateFromRequest()
	{
		parent::updateFromRequest();
	
		global $RM;
		
		$data = parent::getData();
		if ( !isset($data["name"]) )
			return;
			
		$value = $RM->getAndRemove( $data["name"] );
		if ( $value )
			$this->setElementDataString( $value );
	}
	
	public function dumpValue()
	{
		$data = parent::getData();
		$name 	= isset($data["name"])?$data["name"]:"";
		$value 	= isset($data["value"])?$data["value"]:"";
		echo "spInput: name={$name}, value={$value}<br/>";
	}
}

?>